//
//  CMDBSubRegisterTF.swift
//  CMDB
//
//  Created by 王磊 on 05/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBSubRegisterTF: CMDBRegisterTF {
    
    internal var tf: UITextField = UITextField()
    
    internal var placeHolderAttr: NSAttributedString? {
        
        willSet {
            
            tf.attributedPlaceholder = newValue
            
            setNeedsDisplay()
        }
    }
    
    internal var text: String = ""
    
    internal var returnKeyType: UIReturnKeyType = .Default {
        
        willSet {
            
            tf.returnKeyType = newValue
        }
    }
    
    internal var keyboardType: UIKeyboardType = .Default {
        
        willSet {
            
            tf.keyboardType = newValue
        }
        
    }
    internal var maxLength: Int = 11
}
extension CMDBSubRegisterTF {
    
}

extension CMDBSubRegisterTF {
    
    override func addOwnSubviews() {
        super.addOwnSubviews()
        
        addSubview(tf)
    }
    override func configOwnSubviews() {
        super.configOwnSubviews()
        
        tf.delegate = self
        
        tf.returnKeyType = .Default
        
        tf.keyboardType = .Default
        
        tf.textColor = word87Color
        
        tf.font = CMDB_15_FONT
        
        tf.addTarget(self, action: #selector(editingChanged), forControlEvents: .EditingChanged)
    }
}
extension CMDBSubRegisterTF {
    
    override func resignFirstResponder() -> Bool {
        
        return tf.resignFirstResponder()
    }
    override func becomeFirstResponder() -> Bool {
        
        return tf.becomeFirstResponder()
    }
}

extension CMDBSubRegisterTF {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        tf.snp_makeConstraints { [weak self] (make) in
            
            make.left.equalTo(self!.textWidth + 5)
            
            make.right.equalTo(0)
            
            make.centerY.equalTo(5)
        }
    }
}
extension CMDBSubRegisterTF: UITextFieldDelegate {
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        
        if let target = unsureTarget {
            
            let argu = ArguInfo()
            
            argu.view = self
            
            argu.controlEvent = ControlEvent.ReturnKey
            
            TSRuntimeManager.getInstance().addTarget(target, andSel: unsureAction!, andAnyArgu: argu)
            
        }
        
        return true
    }
    
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        
        return range.length == 1 && (string as NSString).length == 0 ? true : (textField.text! as NSString).length < maxLength
        
        //            keyboardType == .NumberPad ? range.length == 1 && (string as NSString).length == 0 ? true : (textField.text! as NSString).length < maxLength : true
    }
}

extension CMDBSubRegisterTF {
    
    @objc private func editingChanged(textField: UITextField) {
        
        text = textField.text!
    }
}